<?php
namespace controllers;
use controllers\api;
use SMB\Statistics\zero_stocks_statistics;
use SMB\Statistics\shop_sales_statistics;
use SMB\Goods\goods;
use Model\suppliers;
use controllers\products;
use SMB\Goods\supplier_goods_relation;
use SMB\Goods\product;
class statistics extends api{
    public function __where(){
        $where = array();
        //接收参数 1 昨日  7近七天  30近30天
        if(isset($_REQUEST['date'])) {
            $sign = (int)$_REQUEST['date'];
            if ($sign == 1) {
                $time = (int)date('Ymd', (time() - 86400));
                $where = array('Ymd' => $time);
            }

            if ($sign == 7) {
                $time = (int)date('Ymd', (time() - 86400 * 7));
                $where = array('Ymd' => array('$gt'=>$time));
            }
            if ($sign == 30) {
                $time = (int)date('Ymd', (time() - 86400 * 30));
                $where = array('Ymd' => array('$gt'=>$time));
            }
        }
        //接收自定义参数
        if(isset($_REQUEST['start'])){
            $start = $_REQUEST['start'];
            $start_time = (int)trim(str_replace('-', '', $start));
            $end = $_REQUEST['end'];
            $end_time = (int)trim(str_replace('-', '', $end));
            $where = array('Ymd'=>array('$gt'=>$start_time,'$lt'=>$end_time));
        }
        return $where;
    }
    //昨日零库存商品数量
    public function zero_stock(){
        //获取供应商信息
        $db = new suppliers();
        $supplier_info = $db->get_sup_info();
        $zero_stocks_statistics_model = new zero_stocks_statistics();
        $end_time = strtotime(date('Y-m-d', time()));
        $start_time = $end_time-86400;
        $querys = array('supplier_id'=>(string)$supplier_info['_id'],'create_time'=>array('$gt'=>$start_time,'$lt'=>$end_time));
        $filer = array('sku_id');
        $zero_stocks = $zero_stocks_statistics_model->find($querys,$filer)->count();
        $data['zero_stocks_count'] = $zero_stocks;
        return $data;
    }

    //昨日商品销售量
    public function sale(){
        //供应商信息
        $supplier_model = new suppliers();
        $supplier_info = $supplier_model->get_sup_info();
        //该供应商下的商品sku_id
        $supplier_relation = new supplier_goods_relation();
        $querys = array('supplier_id'=>$supplier_info['_id']);
        $filer = array('sku_id');
        $relation = $supplier_relation->find($querys,$filer);
        //计算昨日商品销售量
        $shop_sales_statistics_model = new shop_sales_statistics();
        $data = array();
        $time = date('Ymd',(time()-86400));
        //商品聚合 同一商品一天总的销售量
        $shop_sales_statistics_list  = $this->get_sale($shop_sales_statistics_model,$time);
//        $shop_sales_statistics_list = $shop_sales_statistics->aggregate(array(
//            array(
//                '$match'=>array(
//                    'Ymd'=>$time
//                )
//            ),
//            array(
//                '$group'=>array(
//                    '_id'=>array(
//                        'sku_id'=>'$sku_id'
//                    ),
//
//                    'sales_money'=>array('$sum'=>'$sales_money')
//                )
//            )
//        ));

        //获取每个商品的销量
        $shop_sales_statistics_arr = array();
        foreach ($shop_sales_statistics_list['result'] as $shop_sales){
            $shop_sales_statistics_arr['sku_id'] = $shop_sales['sales_money'];
        }
        //计算这个供应商下的商品的销量
        $a = 0;
        foreach ($relation as $v){
            if(isset($shop_sales_statistics_arr[$v['sku_id']])){
                $a += $shop_sales_statistics_arr[$v['sku_id']];
            }
        }
        $data['sale_count'] = $a;
        return $data;
    }

    //昨日销售额明细统计
    public function yesterday_sale(){
        //获取调价
        $where = $this->__where();
        //获取供应商信息
        $suppler_model = new suppliers();
        $supplier_info = $suppler_model->get_sup_info();
        //获取该供应商的商品的sku_id
        $supplier_relation_model = new supplier_goods_relation();
        $querys = array('supplier_id'=>$supplier_info['_id']);
        $filer = array('sku_id');
        $relation = $supplier_relation_model->find($querys,$filer);
        //聚合昨日同一商品销售额
        $shop_sales_statistics_model = new shop_sales_statistics();
        $shop_sales_statistics_list = $shop_sales_statistics_model->aggregate(array(
            array(
                '$match'=>$where
            ),
            array(
                '$group'=>array(
                    '_id'=>array(
                        'sku_id'=>'$sku_id'
                    ),

                    'sales_money'=>array('$sum'=>'$sales_money'),
                    'sales_num'=>array('$sum'=>'$sales_num'),
                    'sku_id'=>array('$first'=>'$sku_id')
                )
            )
        ));

        //获取一个月内每天的销售量
        $date = $this->one_month_num();


        //查询商品信息
        $pro_model = new products();
        $products_arr = $pro_model->get_goods_info();
        //查询商品库存
        $product_st_arr = $this->get_stocks();

        $shop_sales_statistics_arr = array();
        $product_list_arr = array();
        foreach ($shop_sales_statistics_list['result'] as $shop_sales){
            $shop_sales_statistics_arr[$shop_sales['sku_id']]['sales_money'] = $shop_sales['sales_money'];
            $shop_sales_statistics_arr[$shop_sales['sku_id']]['sales_num'] = $shop_sales['sales_num'];
        }
        //根据每个商品sku_id取出库存
        foreach ($product_st_arr['result'] as $vv){
            $product_list_arr[$vv['sku_id']]['stocks'] = $vv['stocks'];
        }

        //重组数组
        $data = array();
        $data_arr = array();
        foreach ($relation as $v){
            if(!isset($products_arr[$v['sku_id']])){
                continue;
            }
            $data_arr['name'] = @$products_arr[$v['sku_id']]['product_title'];
            $data_arr['stock'] = $product_list_arr[$v['sku_id']]['stocks'];
            $data_arr['num'] = isset($shop_sales_statistics_arr[$v['sku_id']]['sales_num'])?$shop_sales_statistics_arr[$v['sku_id']]['sales_num']:0;
            $data_arr['money'] = isset($shop_sales_statistics_arr[$v['sku_id']]['sales_money'])?$shop_sales_statistics_arr[$v['sku_id']]['sales_money']:0;
            array_push($data,$data_arr);
        }
        $this->success($data,$date);
    }
    /*
     * 按时间查询商品的销售量
     * $model 数据表对象
     * $time arrray 加条件
     * */
    public function get_sale($model,$time){
        //商品聚合 同一商品一天总的销售量
        $shop_sales_statistics_list = $model->aggregate(array(
            array(
                '$match'=>array(
                    'Ymd'=>$time
                )
            ),
            array(
                '$group'=>array(
                    '_id'=>array(
                        'sku_id'=>'$sku_id'
                    ),

                    'sales_money'=>array('$sum'=>'$sales_money')
                )
            )
        ));
        return $shop_sales_statistics_list;
    }
    //获取每个商品的库存
    public  function get_stocks(){
        $product_db = new product();
        $product_list = $product_db->aggregate(array(
            array(
                '$group'=>array(
                    '_id'=>array(
                        'sku_id'=>'$sku_id'
                    ),
                    'stocks'=>array('$sum'=>'$stocks'),
                    'sku_id'=>array('$first'=>'$sku_id')
                )
            )
        ));
        return $product_list;
    }
    //获取商品所有信息
    public function get_goods_info(){
        $good_db = new goods();
        $goods = $good_db->find();
        $products = array();
        foreach($goods as $v){
            $products[$v['sku_id']]['product_title'] = @$v;
        }
    }
}